<template>
  <!--看板-->
  <div class="app-container">
    <el-form class="jc_search_wrap" v-model="searchList">
      <label>看板名称</label><el-input v-model="searchList.crystalNo"></el-input>
      <label>所属车间</label><el-input></el-input>
      <el-button @click="searchRows"><i class="el-icon-search"></i>查询</el-button>
    </el-form>
    <el-button-group class="jc_btn_wrap jc_btn_left">
      <el-button @click="editRow"><i class="jc_icon "></i>编辑</el-button>
      <el-button @click="deleteRows"><i class="jc_icon "></i>删除</el-button>
    </el-button-group>
    <el-table :data="list"  :height="tableHeight"  border fit highlight-current-row class="jc_table_wrap"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80px">
      </el-table-column>
      <el-table-column label='看板名称'>
        <template scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label='看板IP'>
        <template scope="scope">
          {{scope.row.ip}}
        </template>
      </el-table-column>
      <el-table-column label='页面链接地址' >
        <template scope="scope">
          {{scope.row.url}}
        </template>
      </el-table-column>
      <el-table-column label='所属车间'>
        <template scope="scope">
          {{scope.row.workshop }}
        </template>
      </el-table-column>
      <el-table-column label='关联设备' >
        <template scope="scope">
          {{scope.row.devices }}
        </template>
      </el-table-column>
      <el-table-column label='看板分辨率' >
        <template scope="scope">
          {{scope.row.resolution }}
        </template>
      </el-table-column>
      <el-table-column label='通讯状态'>
        <template scope="scope">
          {{scope.row.state}}
        </template>
      </el-table-column>
      <el-table-column label='创建人' >
        <template scope="scope">
          {{scope.row.createUser}}
        </template>
      </el-table-column>
      <el-table-column label='创建时间'>
        <template scope="scope">
          {{scope.row.createTime}}
        </template>
      </el-table-column>
    </el-table>
    <!--<el-pagination layout="prev, pager, next" :total="total" @current-change="changePage" :page-size="10"/>-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="changePage"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!--编辑-->
    <el-dialog title="编辑" :visible.sync="edit_visible" class="jc_dialog_wrap">
      <el-form :model="form_edit" label-width="120px">
        <el-form-item label="看板编号">
          <el-input v-model="form_edit.no"></el-input>
        </el-form-item>
        <el-form-item label="看板名称">
          <el-input v-model="form_edit.name"></el-input>
        </el-form-item>
        <el-form-item label="看板IP">
          <el-input v-model="form_edit.ip"></el-input>
        </el-form-item>
        <el-form-item label="分辨率">
          <el-input v-model="form_edit.resolution"></el-input>
        </el-form-item>
        <el-form-item label="所属车间">
          <el-input v-model="form_edit.workshop"></el-input>
        </el-form-item>
        <el-form-item label="选择设备">
          <el-input v-model="form_edit.devices"></el-input>
        </el-form-item>
        <el-form-item label="链接地址">
          <el-input v-model="form_edit.url"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEditForm">确定</el-button>
        <el-button @click="edit_visible=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  import * as Kanban from "../../api/kanban/kanban";

  export default {
    components: {

    },
    data() {
      return {
        searchList: {pageSize: '',crystalNo: '',date1: '',date2: ''},
        list: [],
        total: 0,
        create_visible: false,
        detail_visible: false,
        edit_visible: false,
        form_create: {},
        form_detail: {},
        form_edit: {},
        multipleSelection: [],
        crystalNo: '',
        date1: '',
        date2: '',
        pageSize: 10,
        currentPage: 1,
      }

    },
    created() {
      this.fetchData();
    },
    methods: {
      fetchData(p=1) {
        this.listLoading = true;
        this.searchList.pageNum = this.currentPage = p;
        this.searchList.pageSize = this.pageSize;
        Kanban.getList(this.searchList).then(data => {
          //console.log('data',data.rows);
          this.list = data.rows;
          this.total = parseInt(data.total);
          this.listLoading = false;
        })

      },
      searchRows() {
        this.fetchData()
      },
      editRow() {
        if(this.multipleSelection.length === 0) {
          alert('请先勾选行');
        }else if(this.multipleSelection.length >= 2) {
          alert('每次只能查看一行数据，请只勾选一行');
        }else if(this.multipleSelection.length === 1){
          this.edit_visible = true;
          Kanban.detailRow(this.multipleSelection[0].id).then(data => {
            this.form_edit = data.msg;
          });
        }
      },
      submitEditForm() {
        Kanban.updateRow(this.form_edit).then(data => {
          this.edit_visible = false;
          this.fetchData();
        })
      },
      deleteRows() {
        if(this.multipleSelection.length===0) {
          alert('请选择行');
        }else {
          var ids = [];
          for(var i in this.multipleSelection) {
            ids.push(this.multipleSelection[i].id)
          }
          this.$confirm('此操作将永久删除该行数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            Degum.delRows({ids: ids.join(',')}).then(data => {
              this.fetchData();
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
            })

          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      changePage(p) {
        this.fetchData(p)
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.currentPage = 1;
        this.fetchData(1);

      },
      exportRows() {
        window.location.href=process.env.BASE_API+'/degum/export?crystalNo='+this.searchList.crystalNo+'&date1='+this.searchList.date1+'&date2='+this.searchList.date2;
      },
    },
    computed: {
      tableHeight() {
        const filterHeight = 28
        var tableHeight = this.getTableHeight(filterHeight)
        return tableHeight;
      }
    }
  }
</script>
